from time import sleep
from selenium import webdriver

#打开页面，点击“新闻”
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.find_element_by_link_text('新闻').click()
sleep(3)

#切换到新闻页面，浏览一篇军事文章并关闭
hds = driver.window_handles
driver.switch_to.window(hds[1])
driver.find_element_by_link_text('军事').click()
driver.find_element_by_id("imgView").click()
sleep(3)
print(hds)
driver.switch_to.window(hds[-1])
driver.close()
sleep(3)
hds = driver.window_handles
print(hds)
driver.switch_to.window(hds[-1])
driver.close()


#回到百度首页，搜索 Selenium 关键字
driver.switch_to.window(hds[0])
driver.find_element_by_id("kw").send_keys("Selenium")
button = driver.find_element_by_id("su")
button.click()
sleep(3)

#关闭浏览器
driver.quit()


